import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:pixelyear/utils/constant/AssetsConstant.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:pixelyear/utils/constant/IconConstant.dart';

extension CanvasUtils on Canvas {
  //画文字
  void drawText(String text, TextStyle style, double x, double y) {
    TextSpan span = TextSpan(style: style, text: text);
    TextPainter painter = TextPainter(
        text: span,
        textAlign: TextAlign.left,
        textDirection: TextDirection.ltr);
    painter.layout();
    painter.paint(this, Offset(x, y));
  }

  //画居中文字
  void drawCenterText(
      double boxWidth, String text, TextStyle style, double x, double y) {
    TextSpan span = TextSpan(style: style, text: text);
    TextPainter painter = TextPainter(
        text: span,
        textAlign: TextAlign.left,
        textDirection: TextDirection.ltr);
    painter.layout();

    double getTextTopPadding() {
      return ((boxWidth + painter.height) / 2).toDouble();
    }

    double getTextRightPadding() {
      return ((boxWidth - painter.width) / 2).toDouble();
    }

    painter.paint(
        this, Offset(x + getTextRightPadding(), y - getTextTopPadding()));
  }

  //画居中
  void drawCenterIcon(double boxWidth, int mood, double x, double y,
      double fontSize, Color fontColor) {
    final icon = IconData(mood, fontFamily: AssetsConstant.diyFont);
    TextPainter textPainter = TextPainter(textDirection: TextDirection.rtl);
    textPainter.text = TextSpan(
        text: String.fromCharCode(icon.codePoint),
        style: TextStyle(
            fontSize: fontSize, color: fontColor, fontFamily: icon.fontFamily));

    textPainter.layout();

    double getTextTopPadding() {
      return ((boxWidth + textPainter.height) / 2).toDouble();
    }

    double getTextRightPadding() {
      return ((boxWidth - textPainter.width) / 2).toDouble();
    }

    textPainter.paint(
        this, Offset(x + getTextRightPadding(), y - getTextTopPadding() / 2));
  }

  //画居中
  void drawPaintIcon(
      double boxWidth, double x, double y, double fontSize, Color fontColor) {
    final icon = IconConstant.paint;
    TextPainter textPainter = TextPainter(textDirection: TextDirection.rtl);
    textPainter.text = TextSpan(
        text: String.fromCharCode(icon.codePoint),
        style: TextStyle(
            fontSize: fontSize, color: fontColor, fontFamily: icon.fontFamily));

    textPainter.layout();

    double getTextTopPadding() {
      return ((boxWidth + textPainter.height) / 2).toDouble();
    }

    double getTextRightPadding() {
      return ((boxWidth - textPainter.width) / 2).toDouble();
    }

    textPainter.paint(this,
        Offset(x + getTextRightPadding() * 1.8, y - getTextTopPadding() / 2));
  }
}

extension CustomPainterUtils on CustomPainter {
  //获取文字有多宽
  List drawTextWidthAndHeight(String text, TextStyle style) {
    TextSpan span = TextSpan(style: style, text: text);
    TextPainter painter = TextPainter(
        text: span,
        textAlign: TextAlign.left,
        textDirection: TextDirection.ltr);
    painter.layout();
    return [painter.width, painter.height];
  }
}
